package com.wz.dhs.controller.system;

import com.wz.dhs.model.UserContextHolder;
import com.wz.dhs.model.common.PageInfo;
import com.wz.dhs.model.common.Result;
import com.wz.dhs.service.base.ISysDictDataService;
import com.wz.dhs.service.base.ISysDictTypeService;
import com.wz.dhs.vo.base.SysDictData;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController
{
    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/list")
    public Result<PageInfo<SysDictData>> list(SysDictData dictData)
    {
        PageInfo<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return Result.data(list);
    }


    /**
     * 查询字典数据详细
     */
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictCode}")
    public Result<SysDictData> getInfo(@PathVariable Long dictCode)
    {
        return Result.data(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public Result<List<SysDictData>> dictType(@PathVariable String dictType)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (CollectionUtils.isEmpty(data))
        {
            data = new ArrayList<SysDictData>();
        }
        return Result.data(data);
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @PostMapping
    public Result<String> add(@Validated @RequestBody SysDictData dict)
    {
        dict.setCreateBy(UserContextHolder.getUsername());
        dictDataService.insertDictData(dict);
        return Result.success("保存成功");
    }

    /**
     * 修改保存字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @PutMapping
    public Result<String> edit(@Validated @RequestBody SysDictData dict)
    {
        dict.setUpdateBy(UserContextHolder.getUsername());
        dictDataService.updateDictData(dict);
        return Result.success("保存成功");
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @DeleteMapping("/{dictCodes}")
    public Result<String> remove(@PathVariable Long[] dictCodes)
    {
        dictDataService.deleteDictDataByIds(dictCodes);
        return Result.success("删除成功");
    }
}
